﻿#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using Gonte.Reflection;
using Gonte.UI; 

#endregion

namespace Gonte.Wpf.Controls.ViewModel
{
    public class ViewModelInfo
    {
        #region Methods

        public static ViewModelInfo GetInfo(Type type)
        {
            if (!_viewModelInfos.ContainsKey(type))
            {
                ViewModelInfo viewModelInfo = new ViewModelInfo();
                PopulateViewModelInfo(viewModelInfo, type);

                lock (((ICollection)_viewModelInfos).SyncRoot)
                {
                    _viewModelInfos.Add(type, viewModelInfo);
                }

                return viewModelInfo;
            }

            return _viewModelInfos[type];
        }


        #endregion

        public bool IsContent
        {
            get { return (null != ChildrenProperties); }
        }

        #region Helpers

        private static void PopulateViewModelInfo(ViewModelInfo viewModelInfo, Type type)
        {
            UserInterfaceAttribute viewModelAttribute = CustomAttribute.GetCustomAttribute<UserInterfaceAttribute>(type, false);

            ExceptionThrower.ThrowWhenNull("viewModelAttribute", viewModelAttribute);
            ExceptionThrower.ThrowWhenNullOrEmpty("viewModelAttribute.LabelProperty", viewModelAttribute.LabelProperty);

            viewModelInfo.LabelProperty = viewModelAttribute.LabelProperty;
            viewModelInfo.IconUri = viewModelAttribute.IconUri;
            viewModelInfo.CollapsedIconUri = viewModelAttribute.CollapsedIconUri;
            viewModelInfo.ExpandedIconUri = viewModelAttribute.ExpandedIconUri;

            if (string.IsNullOrEmpty(viewModelInfo.CollapsedIconUri))
            {
                viewModelInfo.CollapsedIconUri = viewModelInfo.IconUri;
            }

            if (string.IsNullOrEmpty(viewModelInfo.ExpandedIconUri))
            {
                viewModelInfo.ExpandedIconUri = viewModelInfo.IconUri;
            }

            UserInterfaceItemAttribute[] childrenAttributes = CustomAttribute.GetCustomAttributes<UserInterfaceItemAttribute>(type, false);

            if (childrenAttributes != null)
            {
                List<string> childrenProperties = new List<string>();

                foreach (UserInterfaceItemAttribute childrenAttribute in childrenAttributes)
                {
                    if (childrenAttribute != null
                        && !string.IsNullOrEmpty(childrenAttribute.PropertyName))
                    {
                        childrenProperties.Add(childrenAttribute.PropertyName);
                    }
                }

                viewModelInfo.ChildrenProperties = new string[childrenProperties.Count];
                childrenProperties.CopyTo(viewModelInfo.ChildrenProperties);
            }
        }

        #endregion

        #region Fields

        /// <summary>
        /// The name of the object property that maps to the label
        /// </summary>
        public string LabelProperty;

        /// <summary>
        /// The uri for the icon image that is only one for the ItemViewModel
        /// but ir can be used as the common image (expanded and collapsed) for the ContentViewModel
        /// </summary>
        public string IconUri;

        /// <summary>
        /// The uri of the collapsed icon image
        /// </summary>
        public string CollapsedIconUri;

        /// <summary>
        /// The uri of the expanded icon image
        /// </summary>
        public string ExpandedIconUri;

        /// <summary>
        /// The name of the properties that map to children in the view model
        /// </summary>
        public string[] ChildrenProperties;

        public delegate ContextMenu ContextMenuHandler(object @object);

        public ContextMenuHandler ContextMenuFactory;

        // Cached view model info
        private static readonly Dictionary<Type, ViewModelInfo> _viewModelInfos = new Dictionary<Type, ViewModelInfo>();

        #endregion

        internal void PopulateViewModelInfo(ViewModelInfo viewModelInfo, string viewModelInfoName)
        {
            throw new NotImplementedException();
        }
    }
}
